//
// Created by Felix on 24-4-26.
//

#ifndef GAZE_LOGINLOGDB_HPP
#define GAZE_LOGINLOGDB_HPP

#include "oatpp-sqlite/orm.hpp"

#include "dto/AuthResponseDto.hpp"

#include OATPP_CODEGEN_BEGIN(DbClient)

class LoginLogDb : public oatpp::orm::DbClient {
public:
    explicit LoginLogDb(
            const std::shared_ptr<oatpp::orm::Executor> &executor
    ) : oatpp::orm::DbClient(executor) {
        oatpp::orm::SchemaMigration migration(executor);
        auto version = executor->getSchemaVersion();
        OATPP_LOGD("LoginLogDb", "Migration - OK. Version=%lld.", version)
    }

    QUERY(createLoginLog,
          "INSERT INTO login_log "
          "(user_id, token, resource_token, refresh_token, status, create_time, update_time, delete_time) VALUES "
          "(:auth.user_id, :auth.token, :auth.resource_token, :auth.refresh_token, 1, :auth.create_time, :auth.update_time, :auth.delete_time) "
          "RETURNING id;",
          PREPARE(true), // prepared statement!
          PARAM(oatpp::Object<AuthResponseDto>, auth)
    )

    QUERY(updateLoginLogStatus,
          "UPDATE login_log "
          "set status = :status where token = :token;",
          PREPARE(true), // prepared statement!
          PARAM(oatpp::Int8, status),
          PARAM(oatpp::String, token)
    )

    QUERY(getActiveLoginLogByToken,
          "SELECT user_id FROM login_log "
          "WHERE status = 1 and delete_time = 0 and token = :token;",
          PREPARE(true), // prepared statement!
          PARAM(oatpp::String, token)
    )
};

#include OATPP_CODEGEN_END(DbClient)

#endif //GAZE_LOGINLOGDB_HPP
